<?php

namespace App\Http\Controllers\Admin;

use App\Model\Advloction;
use App\Model\Billboard;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class BillboardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Billboard::orderBy('id','desc')->paginate(15);
        return view('admin.adv.billboard-list',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $classify = Advloction::orderBy('id','asc')->pluck('title','id');
        return view('admin.adv.billboard-add',compact('classify'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required'=>'请填写广告名称'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        if ($request->hasFile('file')){
            $file = $request->file('file');
            $fileName = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$fileName)){
                return $this->error('不支持的图片格式');
            }
            $path = $file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            return $this->error('请上传广告图片');
        }
        $start_data = array();
        $end_data = array();
        $start_data[] = $request->input('start-data');
        $end_data[] = $request->input('end-data');
        $data = Billboard::create(
        [
            'title'=>$request->input('title'),
            'classify'=>$request->input('classify'),
            'start_data'=>$start_data,
            'end_data'=>$end_data,
            'img_url'=>$path,
            'link'=>$request->input('link'),
            'state'=>$request->input('state')
        ]);
        if ($data){
            return $this->success('添加成功');
        }else{
            return $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item = Billboard::where('id',$id)->first();
        $classify = Advloction::orderBy('id','asc')->pluck('title','id');
        return view('admin.adv.billboard-edit',compact('item','classify'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required' => '广告名称不能为空'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        if ($request->hasFile('file')){
            $file = $request->file('file');
            $validArr = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$validArr)){
                return $this->error('不支持的图片格式');
            }
            $path = $file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            $img = $request->input('img');
        }
        $m = Billboard::find($id);
        $start_data = array();
        $end_data = array();
        $start_data[] = $request->input('start-data');
        $end_data[] = $request->input('end-data');
        $m->title = $request->input('title');
        $m->classify = $request->input('classify');
        $m->start_data = $start_data;
        $m->end_data = $end_data;
        $m->img_url = !empty($path) ? $path : $img;
        $m->link = $request->input('link');
        $m->state = $request->input('state');
        if ($m->save()){
            return $this->success('更改成功');
        }else{
            return $this->error('更改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Billboard::where('id',$id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}
